﻿@{
    ViewBag.Title = "Productupdate";
    Layout = null;
}


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>物料修改</title>

    <script src="~/libs/jquery/jquery.js"></script>
    <link href="//unpkg.com/layui@2.9.16/dist/css/layui.css" rel="stylesheet">
    <script src="//unpkg.com/layui@2.9.16/dist/layui.js"></script>

</head>
<body>
    <h1>物料修改</h1>
    <form class="layui-form layui-form-pane" >
        <div class="layui-form" style="padding: 20px;">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">* 物料编码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="productCode" id="ProductCode" lay-verify="required" placeholder="请输入物料编码" autocomplete="off" class="layui-input" disabled>
                        <input type="hidden" name="Id" id="Id">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">* 物料名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="productName" id="productName" lay-verify="required" placeholder="请输入物料名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">规格型号</label>
                <div class="layui-input-block">
                    <input type="text" name="specifiCation" id="specifiCation" placeholder="请输入规格型号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">* 单位</label>
                    <div class="layui-input-inline">
                        <select name="unitName" id="unitName" lay-verify="required">
                            <option value="">请选择单位</option>
                            <option value="kg">千克</option>
                            <option  value="g">克</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否启用</label>
                <div class="layui-input-block">
                    <input type="radio" id="IsEnable" name="IsEnable" value="true" title="是" checked>
                    <input type="radio" id="IsEnable" name="IsEnable" value="false" title="否">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">安全库存</label>
                <div class="layui-input-block">
                    <input type="radio" id="isStock" name="isStock" value="true" title="是">
                    <input type="radio" id="isStock" name="isStock" value="false" title="否" checked>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <input type="text" name="remark" id="remark" placeholder="请输入内容" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" type="submit" lay-submit lay-filter="btnUpdata">确定</button>
                    <button type="button" class="layui-btn layui-btn-primary" onclick="closeModal()">取消</button>
                </div>
            </div>
        </div>
    </form>


    <script>
        layui.use(['form', 'jquery', 'element'], function () {
            var form = layui.form,
                $ = layui.jquery;
            $ = layui.$;
            // 监听确认按钮点击事件





            $(document).ready(function () {

                getFormData();
            });
            const productId = sessionStorage.getItem("id");
            function getFormData() {
               
                console.log(productId);

                $.ajax({
                    url: `/Wyx/GetProductByIdData`,
                    type: "GET",
                    data: {
                        id: productId
                    },
                    dataType: "json",
                    success: function (res) {
                   
                       

                    
                     
                            // 反填表单

                        $('#ProductCode').val(res.productCode); // 修复了这里的错误
                        $('#productName').val(res.productName);
                        $('#specifiCation').val(res.specifiCation);
                        $('#unitName').val(res.unitName).prop('selected', false); // 明确设为选中
                        $('#remark').val(res.remark);
                        form.render('select'); // 手动刷新select控件的渲染
                        $('input[name="IsEnable"][value="' + res.IsEnable + '"]').prop('checked', true);
                        $('input[name="isStock"][value="' + res.isStock + '"]').prop('checked', true);
                    },
                    error: function (error) {
                        console.error("AJAX 请求失败", error);
                    }
                });
            }






            form.on('submit(btnUpdata)', function (data) {
                // 获取表单数据
              

           

                // 发送 AJAX 请求
                //console.log
                $.ajax({
                    url: '/Wyx/UpdateProductData', // 根据路由修改为实际路径
                    type: 'get', // 请求类型
                    contentType: 'json', // 设置请求内容类型为JSON
                    data: {
                        ProductCode: $('#ProductCode').val(),
                        ProductName: $('#productName').val(),
                        SpecifiCation: $('#specifiCation').val(),
                        UnitName: $('#unitName').val(),
                        IsEnable: $('input[name="IsEnable"]:checked').val(),
                        IsStock: $('input[name="isStock"]:checked').val(),
                        Remark: $('#remark').val(),
                        Id: productId
                    }, // 表单数据
                    success: function (res) {
                      
                        if (res) {
                            console.log(res);
                            layer.msg('修改成功', {
                                icon: 1,
                                time: 1000
                            }, function () {
                               
                            });

                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index); 

                        }
                         
                    },
                    error: function (xhr, status, error) {
                        console.error(error); // 打印错误信息
                        alert('请求失败: ' + error); // 提示请求失败
                    }
                });
            });


        });
    </script>

</body>
</html>